package com.mdp.workflow.de.ctrl;

import com.mdp.core.entity.LangTips;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.workflow.de.entity.Model;
import com.mdp.workflow.de.service.ModelServiceImpl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

/**
 * url编制采用rest风格,如对WF.act_de_model的操作有增删改查,对应的url分别为:<br>
 *  新增: workflow/de/model/add <br>
 *  查询: workflow/de/model/list<br>
 *  模糊查询: workflow/de/model/listKey<br>
 *  修改: workflow/de/model/edit <br>
 *  删除: workflow/de/model/del<br>
 *  批量删除: workflow/de/model/batchDel<br>
 * 组织 com.qqkj  顶级模块 mdp 大模块 workflow 小模块 de<br>
 * 实体 Model 表 WF.act_de_model 当前主键(包括多主键): id; 
 ***/
@Controller("mdp.workflow.de.modelDiagramController") 
public class ModelDiagramController {
	
	static Log logger=LogFactory.getLog(ModelDiagramController.class);
	
	@Autowired
	ModelServiceImpl modelService;
		
	@Autowired	
	SpringProcessEngineConfiguration engineConfig;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;
	/**
	* 获得流程图的流
	* @return
	*/
	@RequestMapping(value="/**/workflow/de/model/diagram/{id}", method = RequestMethod.GET)
	public void getModelDiagram(@PathVariable String id,HttpServletResponse response){
		Model m=this.modelService.getModel(id);
		BpmnModel bpmnModel = this.modelService.getBpmnModel(m);
		ProcessDiagramGenerator diagramGenerator = engineConfig.getProcessDiagramGenerator();
	    InputStream is = diagramGenerator.generateDiagram(bpmnModel, "bmp", Collections.<String>emptyList(), Collections.<String>emptyList(), engineConfig.getActivityFontName(),
	        		engineConfig.getLabelFontName(), engineConfig.getAnnotationFontName(), engineConfig.getClassLoader(), 1.0, true);
			 
		try {
			IOUtils.copy(is, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
